package com.xxx.dao;

import com.xxx.pojo.Student;
import com.xxx.utils.DBUtils;
import com.xxx.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/8/5 15:49
 * @Copyright 博为峰
 */
public class StudentDaoImpl implements StudentDao {
    @Override
    public ArrayList<Student> query() {
        try (Connection connection = DBUtils.getConnection();
             final PreparedStatement preparedStatement = connection.prepareStatement("select * from stu");
        ) {
            final ResultSet resultSet = preparedStatement.executeQuery();
            ArrayList<Student> list = new ArrayList<Student>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                double height = resultSet.getDouble("height");
                String sex = resultSet.getString("sex");
                String birthday = resultSet.getString("birthday");
                int claId = resultSet.getInt("cla_id");
                list.add(new Student(id, name, age, height, sex, birthday, claId));
            }
            return list;
        } catch (SQLException throwables) {
            throw new RuntimeException("查询数据异常");
        }
    }
}
